package cn.alcatraz.mq_05_tran;

import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * @Description: 生产者
 * @Author: Alcatraz
 * @Version: 1.0
 * @Date:  2022/7/26 21:11
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        // 创建事务消息生产者
        TransactionMQProducer tran_producer_group = new TransactionMQProducer("tran_producer_group");

        // 连接nameserver
        tran_producer_group.setNamesrvAddr("127.0.0.1:9876");

        // 启动
        tran_producer_group.start();

        // 指定事务监听器
        tran_producer_group.setTransactionListener(new MyTranIistener());

        // 发送事务消息
        Message message = new Message("tran_topic", "tran", "测试事务消息".getBytes(StandardCharsets.UTF_8));
        TransactionSendResult transactionSendResult = tran_producer_group.sendMessageInTransaction(message,null);
        System.out.println(transactionSendResult);

    }
}
